from flask import Blueprint, request, session
from flask_restful import Api, Resource, marshal_with
from flask_restful.reqparse import RequestParser

from webapp.config import api_utils
from webapp.config.api_utils import oper_log, check_auth
from webapp.dto_mapper import course_page_fields, course_table_fields
from webapp.schema import GradeSchema, CourseSchema
from webapp.service.course_service import CourseService
from webapp.service.grade_service import GradeService

course_app = Blueprint('course', __name__, url_prefix='/api/course')
course_api = Api(course_app)

parser = RequestParser()
parser.add_argument('id', location='json', type=int, trim=True, required=True)
parser.add_argument('cname', location='json', type=str, trim=True, required=False)
parser.add_argument('plan', location='json', type=int, trim=True, required=False)
parser.add_argument('teachclass', location='json', type=int, trim=True, required=False)
parser.add_argument('startS', location='json', type=int, trim=False, required=False)
parser.add_argument('startE', location='json', type=int, trim=False, required=False)
parser.add_argument('placetype', location='json', type=int, trim=False, required=False)
parser.add_argument('teacher', location='json', type=int, trim=False, required=False)
parser.add_argument('sessionId', location='json', type=int, trim=False, required=False)
parser.add_argument('campus', location='json', type=int, trim=False, required=False)


@course_api.resource("", "/")
class CourseApi(Resource):

    @oper_log('查询所有课程', request)
    @check_auth('course:list', request)
    @marshal_with(course_page_fields)
    def get(self):
        params = request.args.items(multi=True)
        params_dict = api_utils.build_params(params)
        return CourseService.query_all(**params_dict)

    @oper_log('删除课程', request)
    @check_auth('course:del', request)
    def delete(self):
        ids = request.get_json()
        course_set = set()
        for _id in ids:
            course_set.add(CourseService.find_by_id(id=_id))
        CourseService.delete(course_set=course_set)

    @oper_log('新增课程', request)
    @check_auth('course:add', request)
    def post(self):
        parser_post = parser.copy()
        parser_post.remove_argument('id')
        parser_post.parse_args()
        params = request.get_json()
        schema = CourseSchema()
        curr_course = schema.load(params, unknown="exclude")
        user = session["user"]
        curr_course.create_by = user["username"]
        CourseService.create(curr_course=curr_course)

    @oper_log('修改课程', request)
    @check_auth('course:edit', request)
    def put(self):
        parser_put = parser.copy()
        parser_put.parse_args()
        params = request.get_json()
        schema = CourseSchema()
        curr_course = schema.load(params, unknown="exclude")
        user = session["user"]
        curr_course.update_by = user["username"]
        CourseService.update(curr_course=curr_course)

@course_api.resource("", "/<int:id>")
class CourseEditApi(Resource):
    @oper_log('查询单个课程', request)
    @check_auth('course:list', request)
    @marshal_with(course_table_fields)
    def get(self, id):
        return CourseService.find_by_id(id)